/*
 * FileName:    Fomart.java
 * Description: Fomart 实现
 * History:     2010-8-23 (Liaobj) 1.0 Create
 */
package com.sn.system.common.constants;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Fomart {

    public static String YYYYMMDD_NOW(Object date) {
    	return Constants.DATE_FORMAT4.format(new Date());
    }
    public static String PASSWORD(Object data) {
    	return "P"+data.toString();
    }
    public static String BYTE20(Object data) {
    	if(data!=null&&!data.toString().equals("")){
    		return substringByBytes(data.toString(),20);
    	}
    	else{
    		return "";
    	}
    }
    public static String BYTE12(Object data) {
    	if(data!=null&&!data.toString().equals("")){
    		return substringByBytes(data.toString(),12);
    	}
    	else{
    		return "";
    	}
    }
    public static String BYTE13(Object data) {
    	if(data!=null&&!data.toString().equals("")){
    		return substringByBytes(data.toString(),13);
    	}
    	else{
    		return "";
    	}
    }
    public static String BYTE200(Object data,Object data2,Object data3) {
    	if(data==null||data.toString().equals("")){
    		data="";
    	}
    	if(data2==null||data2.toString().equals("")){
    		data2="";
    	}
    	if(data3==null||data3.toString().equals("")){
    		data3="";
    	}
    	return substringByBytes((data.toString()+" "+data2.toString()+" "+data3.toString()),200);
    }
    public static String YYYYMMDD(Object date) throws ParseException {
    	if(date!=null){
        	return Constants.DATE_FORMAT4.format(date);
    	}else{
    		return "";
    	}

    }
    public static String POSTALCODE(Object data) {
    	if(data!=null&&data.toString().length()==7){
    		return data.toString().substring(0,3)+"-"+data.toString().substring(3,7);
    	}else{
    		data="";
    	}
    	return data.toString();
    }
    public static String FIX_0(Object data) {
    	return "0";
    }
    public static String FIX_1(Object data) {
    	return "1";
    }


    /**
     * 固定データを長さになって  不足の 0補
     * @param n  目標の長さ
     * @param sid  データ
     * @return  データ
     */
    public static String CHALEN(Object n,Object sid) {
    	return String.format("%1$0"+n+"d", Integer.parseInt(sid.toString()));
    }

    public static String SEX(Object sex){
    	String o_sex="";
    	if(sex!=null){
    		if(sex.toString().trim().equals("m")){
    			o_sex="男";
    		}else if(sex.toString().trim().equals("w")){
    			o_sex="女";
    		}else{
    			o_sex=sex.toString();
    		}
    	}
    	return o_sex;
    }

    public static String YMD_YMD(Object date) {
    	return date.toString().substring(0, 10);
    }


    public static String dealTwo(Object danwei,Object name) {

        return name.toString()+"~~"+danwei.toString();
    }

    public static String YMD_BR(Object date) {
    	if(date.toString().length()==21){
    		date=date.toString().substring(0, 4)+"/"+date.toString().substring(5, 7)+"/"+date.toString().substring(8, 10);
    	}
    	return date.toString();
    }

    /**
    *
    * @param date
    * @return YYYY/MM/DD HH:MI:SS
    */
   public static String YMD_HMS(Object date) {
	   if(date.toString().length()==21){
   		date=date.toString().substring(0, 4)+"/"+date.toString().substring(5, 7)+"/"+date.toString().substring(8, 19);
   	}
   	return date.toString();
   }

    /**年齢
    *
    * @param Object
    * @return 年齢
    */
    public static String COMAGE(Object date) {
    	if(date.toString().length()==21){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	    	String cYear = sdf.format(new Date()).substring(0,4);
	    	String birthYear = date.toString().substring(0,4);

	    	int cMoon =Integer.parseInt(sdf.format(new Date()).substring(5,7));
	    	int birthMoon =Integer.parseInt(date.toString().substring(5,7));
	    	
	    	int cday =Integer.parseInt(sdf.format(new Date()).substring(8,10));
	    	int birthday =Integer.parseInt(date.toString().substring(8,10));

	    	int age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
	    	int moon=0;

	    	if(cMoon>birthMoon){
	    		moon=cMoon-birthMoon;
	    	}else if(cMoon<birthMoon){
	    		moon=12-birthMoon+cMoon;
	    		age=age-1;
	    	}
	    	
	    	if(cday-birthday<-1){
	    		cMoon-=1;
	    		if(cMoon>birthMoon){
		    		moon=cMoon-birthMoon;
		    	}else if(cMoon<birthMoon){
		    		moon=12-birthMoon+cMoon;
		    		age=age-1;
		    	}
	    	}

	    	return Integer.toString(age);
	    	}
    	return "";
    }

    /**詳しい年齢 計算
    *
    * @param Object
    * @return 例 ：29歳3ヶ月
    */
    public static String MOONAGE(Object date) {
    	if(date.toString().length()==21){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	    	String cYear = sdf.format(new Date()).substring(0,4);
	    	String birthYear = date.toString().substring(0,4);

	    	int cMoon =Integer.parseInt(sdf.format(new Date()).substring(5,7));
	    	int birthMoon =Integer.parseInt(date.toString().substring(5,7));
	    	
	    	int cday =Integer.parseInt(sdf.format(new Date()).substring(8,10));
	    	int birthday =Integer.parseInt(date.toString().substring(8,10));

	    	int age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
	    	int moon=0;

	    	if(cMoon>birthMoon){
	    		moon=cMoon-birthMoon;
	    	}else if(cMoon<birthMoon){
	    		moon=12-birthMoon+cMoon;
	    		age=age-1;
	    	}
	    	
	    	if(cday-birthday<-1){
	    		cMoon-=1;
	    		if(cMoon>birthMoon){
		    		moon=cMoon-birthMoon;
		    	}else if(cMoon<birthMoon){
		    		moon=12-birthMoon+cMoon;
		    		age=age-1;
		    	}
	    	}

	    	return Integer.toString(age)+"歳"+Integer.toString(moon)+"ヶ月";
    	}
    	return "";
    }
    /**詳しい年齢 計算
    *
    * @param  Date
    * @return 例 ：29歳3ヶ月
    */
    public static String MOONAGEX(Date date) {
    	if(date.toString().length()==21){
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	    	String cYear = sdf.format(new Date()).substring(0,4);
	    	String birthYear = date.toString().substring(0,4);

	    	int cMoon =Integer.parseInt(sdf.format(new Date()).substring(5,7));
	    	int birthMoon =Integer.parseInt(date.toString().substring(5,7));
	    	
	    	int cday =Integer.parseInt(sdf.format(new Date()).substring(8,10));
	    	int birthday =Integer.parseInt(date.toString().substring(8,10));

	    	int age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
	    	int moon=0;

	    	if(cMoon>birthMoon){
	    		moon=cMoon-birthMoon;
	    	}else if(cMoon<birthMoon){
	    		moon=12-birthMoon+cMoon;
	    		age=age-1;
	    	}
	    	
	    	if(cday-birthday<-1){
	    		cMoon-=1;
	    		if(cMoon>birthMoon){
		    		moon=cMoon-birthMoon;
		    	}else if(cMoon<birthMoon){
		    		moon=12-birthMoon+cMoon;
		    		age=age-1;
		    	}
	    	}

	    	return Integer.toString(age)+"歳"+Integer.toString(moon)+"ヶ月";
    	}
    	return "";
    }
    /**年
    *
    * @param date1,date2
    * @return 例：2008/08/08~2010/01/01
    */
    public static String ONLYYEAR(Date date){
    	int year=0;
    	if(date.toString().length()==21){
    		year=Integer.parseInt(date.toString().substring(0,4));

    	}
    	return Integer.toString(year);
    }

    /**有効な期日
    *
    * @param date1,date2
    * @return 文字列を出力します 例：2008/08/08~2010/01/01
    */
    public static String YMD_SE(Object sdate, Object edate){
    	if(sdate.toString().length()==21&&edate.toString().length()==21){
    		sdate=sdate.toString().substring(0, 4)+"/"+sdate.toString().substring(5, 7)+"/"+sdate.toString().substring(8, 10);
    		edate=edate.toString().substring(0, 4)+"/"+edate.toString().substring(5, 7)+"/"+edate.toString().substring(8, 10);
    	}
    	return sdate.toString()+"~"+edate.toString();
    }
    /**
    * 氏名
    * @param first,last
    * @return
    */
    public static String SHOWNAME(Object first, Object last){
    	if(first==null){
    		first="";
    	}
    	if(last==null){
    		last="";
    	}
    	return first.toString()+"　"+last.toString();
    }

    public static String SHOWNAMEX(String first, String last){

    	if(first==null){
    		first="";
    	}
    	if(last==null){
    		last="";
    	}
    	return first.toString()+" "+last.toString();
    }

    public static String RESEARCHNAME(Object names, Object frees){
    	names=names.toString().replaceAll(",{2,}", ",");
    	frees=frees.toString().replaceAll(",{2,}", ",");
    	if(names.toString().equals(",")){
    		names="";
    	}
    	if(frees.toString().equals(",")){
    		frees="";
    	}
    	String o_str=substringByBytes((names.toString()+frees.toString()),100);
    	if (o_str.endsWith(",")){
    		o_str=o_str.substring(0, o_str.length()-1);
    	}
    	if (o_str.startsWith(",")){
    		o_str=o_str.substring(1, o_str.length());
    	}
    	return o_str;
    }

    
    public static String substringByBytes(String i_str, int length){
		return leftStr(i_str, length, -1);

    }

    public static String leftStr(String source, int maxByteLen, int flag){
        if(source == null || maxByteLen <= 0){
            return "";
        }
        byte[] bStr;
		try {
			bStr = source.getBytes("Windows-31J");

        if(maxByteLen >= bStr.length){
        	return source;
        }
        String cStr = new String(bStr, maxByteLen - 1, 2,"Windows-31J");
        if(cStr.length() == 1 && source.contains(cStr)){
            maxByteLen += flag;
        }
        return new String(bStr, 0, maxByteLen,"Windows-31J");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return source;
    }
    
    public static String ID(){
    	String dataStr=String.valueOf(System.currentTimeMillis());
    	return Constants.md5Encoding(dataStr).toUpperCase();
    }

}
